﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.POLL.Components;
using System.IO;

namespace ASPNETVN.PORTAL.Modules.POLL.Controls
{
    public partial class AddEditIPoll : AddEditItemControl
    {
         protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {                
                //lblSave.Text = "";
                if (this.ItemID == 0)
                {
                    ViewState["QuestionID"] = 0;
                }
                else
                {
                    ViewState["QuestionID"] = this.ItemID;
                    btQuestion.Text = "Sửa";
                    divAnswer.Visible = true;
                }
                LoadData();
                if (chkStatus.Checked) chkStatus.Enabled = false;
            }

        }
        
        /// <summary>
        /// Load Question, Answers correspond with loaded question
        /// </summary>
        public void LoadData()
        {
            DataTable dtAnswer=new DataTable();
            //Update
            if (Int32.Parse(ViewState["QuestionID"].ToString()) > 0)
            {
                Components.Question question = Components.Question.Load(Int32.Parse(ViewState["QuestionID"].ToString()));
                txtQuestion.Text = question.Description;
                chkStatus.Checked = question.Status;
                dtAnswer = Answer.SelectBy_QuestionID(Int32.Parse(ViewState["QuestionID"].ToString())).Tables[0];
                if (dtAnswer.Rows.Count > 0)
                {
                    gvAnswer.DataSource = dtAnswer;
                    gvAnswer.DataBind();
                }
                else
                {
                    dtAnswer.Rows.Add(dtAnswer.NewRow());
                    gvAnswer.DataSource = dtAnswer;
                    gvAnswer.DataBind();

                    int totalColumns = gvAnswer.Rows[0].Cells.Count;
                    gvAnswer.Rows[0].Cells.Clear();
                    gvAnswer.Rows[0].Cells.Add(new TableCell());
                    gvAnswer.Rows[0].Cells[0].ColumnSpan = totalColumns;
                    gvAnswer.Rows[0].Cells[0].Text = "Chưa có câu trả lời"; 
                }
            }
            //insert
            else
            {
                DataColumn answerCol = dtAnswer.Columns.Add("ID", typeof(Int32));
                //answerCol.AllowDBNull = false;
                //answerCol.Unique = true;

                dtAnswer.Columns.Add("Description", typeof(String));

                dtAnswer.Rows.Add(dtAnswer.NewRow());
                gvAnswer.DataSource = dtAnswer;
                gvAnswer.DataBind();

                int totalColumns = gvAnswer.Rows[0].Cells.Count;
                gvAnswer.Rows[0].Cells.Clear();
                gvAnswer.Rows[0].Cells.Add(new TableCell());
                gvAnswer.Rows[0].Cells[0].ColumnSpan = totalColumns;
                gvAnswer.Rows[0].Cells[0].Text = "Thêm câu hỏi trước khi thêm câu trả lời";
            }
        }
        /// <summary>
        /// Add new question
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btQuestion_Click(object sender, EventArgs e)
        {
            lblSave.Text = "";
            Components.Question question = new Components.Question();
            question.ModuleID = this.ModuleID;
            question.Description = txtQuestion.Text.Trim();
            question.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;
            question.CreatedDate = DateTime.Now;
            if (Int32.Parse(ViewState["QuestionID"].ToString()) == 0)
            {
                this.ItemID = question.Insert();
                ViewState["QuestionID"] = this.ItemID;
            }
            else
            {
                question.ID = Int32.Parse(ViewState["QuestionID"].ToString());
                question.Update();
            }            
            btQuestion.Text = "Sửa";
            LoadData();
            divAnswer.Visible = true;

        }

        protected void gvAnswer_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            
            if (e.CommandName.Equals("AddNew"))
            {
                TextBox txtAddContent = (TextBox)gvAnswer.FooterRow.FindControl("txtAddContent");                
                if (txtAddContent.Text.Trim().Length != 0)
                {
                    if (Int32.Parse(ViewState["QuestionID"].ToString()) == 0)
                    {
                        lblSave.Text = "Lưu câu hỏi trước khi thêm câu trả lời";
                    }
                    else
                    {
                        Components.Answer answer = new Components.Answer();
                        answer.QuestionID = Int32.Parse(ViewState["QuestionID"].ToString());
                        answer.Description = txtAddContent.Text.Trim();
                        answer.Amount = 0;
                        answer.Insert();
                        
                    }
                }
                LoadData();

            }
        }

        /// <summary>
        /// Switch a specific row of the GridView to Edit Mode
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvAnswer_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvAnswer.EditIndex = e.NewEditIndex;
            LoadData();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvAnswer_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvAnswer.EditIndex = -1;
            LoadData();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvAnswer_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtContent = (TextBox)gvAnswer.Rows[e.RowIndex].FindControl("txtContent");
            Components.Answer.UpdateDes(Convert.ToInt32(gvAnswer.DataKeys[e.RowIndex].Value), txtContent.Text.Trim());
            gvAnswer.EditIndex = -1;
            LoadData();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvAnswer_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                Components.Answer.DeleteAnswer(Convert.ToInt32(gvAnswer.DataKeys[e.RowIndex].Value));
                LoadData();
            }
            catch { }
        }

        protected void chkStatus_CheckedChanged(object sender, EventArgs e)
        {            
            if (chkStatus.Checked)
            {
                chkStatus.Enabled = false;
                Components.Question.UpdateStatus(this.ModuleID, Int32.Parse(ViewState["QuestionID"].ToString()));
                LoadData();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
       
        protected void btnAddQuestion_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            ViewState["QuestionID"] = 0;
            txtQuestion.Text = "";
            btQuestion.Text="Lưu";
            divAnswer.Visible = false;
            chkStatus.Enabled = true;
        }
    }
}